﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsusercategoryServices : Repository<corecmsusercategory>, IcorecmsusercategoryServices
    {
        public bool InsertAsync(corecmsusercategory entity)
        {
            return base.Change<corecmsusercategory>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsusercategory> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsusercategory entity)
        {
            return base.Change<corecmsusercategory>().Update(a => new corecmsusercategory()
            {
                corpId = entity.corpId,
                storeId = entity.storeId,
                name = entity.name,
                sort = entity.sort,
                isShow = entity.isShow,
                createTime = entity.createTime,
                isDelete = entity.isDelete,
                sharingCode = entity.sharingCode,
                imageUrl = entity.imageUrl,
                modifyTime = entity.modifyTime,
                isDeposit = entity.isDeposit,
                isIntegral = entity.isIntegral,
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsusercategory> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsusercategory>().Update(a => new corecmsusercategory()
                {
                    corpId = item.corpId,
                    storeId = item.storeId,
                    name = item.name,
                    sort = item.sort,
                    isShow = item.isShow,
                    createTime = item.createTime,
                    isDelete = item.isDelete,
                    sharingCode = item.sharingCode,
                    imageUrl = item.imageUrl,
                    modifyTime = item.modifyTime,
                    isDeposit = item.isDeposit,
                    isIntegral = item.isIntegral,
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsusercategory>().Update(a => new corecmsusercategory()
            {
                isDelete = true
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsusercategory>().Update(a => new corecmsusercategory()
                {
                    isDelete = true
                }, a => a.id == id);
            }
            return returnKey;
        }


        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsusercategory> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        public List<corecmsusercategory> SelectByCorpId(int corpId, int storeId)
        {
            return base.GetList(a => a.corpId == corpId && a.storeId == storeId && a.isDelete == false).OrderBy(a => a.id).ToList();
        }
        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsusercategory> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsusercategory> custos = new List<corecmsusercategory>();
            var where = Expressionable.Create<corecmsusercategory>();
            where = where.And(a => a.isDelete == false);
            if (!string.IsNullOrEmpty(sName))
            {
                where = where.And(a => a.name.Contains(sName))
                    .Or(a => a.id.Equals(sName));
            }
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.sort).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.sort).ToList();
            }
            return custos;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsusercategory> QueryAsync(long typeId)
        {
            List<corecmsusercategory> list = new List<corecmsusercategory>();
            var where = Expressionable.Create<corecmsusercategory>();
            where = where.And(a => a.isDelete == false && a.isShow == true && a.id > 0);
            if (typeId > 0)
            {
                where = where.And(a => a.id == typeId);
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.sort).ToList();
            return list;
        }
        #endregion
    }
}